//
//  TopicTableView.m
//  yilutongxing
//
//  Created by ios-23 on 17/4/5.
//  Copyright © 2017年 YZE. All rights reserved.
//
#import "MJRefresh+FooterManger.h"
#import "TopicTableView.h"
#import "YZECommunityTableViewCell2.h"
#import "TopicDetailViewController.h"

#define PageUrl @"/api/topics"

@interface TopicTableView ()

@end

@implementation TopicTableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate=self;
        self.dataSource=self;
        self.backgroundColor = WHITE_COLOR;
        self.showsVerticalScrollIndicator = NO;
    
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self registerNib:[UINib nibWithNibName:@"YZECommunityTableViewCell2" bundle:[NSBundle mainBundle]]
               forCellReuseIdentifier:@"YZECommunityTableViewCell2"];
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 15)];
        view.backgroundColor = COLOR_ALPHA(238, 238, 238, 1.0);
        
        self.tableHeaderView=view;
        
        self.mj_header=[RefershHelper headerWithRefreshingBlock:^{
            [[YZENetwork sharedInstace]getRequestWithUrl:[BASE stringByAppendingString:PageUrl]
                                              parameters:nil
                                                delegate:self
                                              requestTag:1
                                            successBlock:^(id responseObject)
            {
                self.topicArr=[YZEModel mj_objectArrayWithKeyValuesArrayAdd:responseObject];
                [self reloadData];
                self.footRefreshState = MJFooterRefreshStateLoadMore;
            }];
           
            [self.mj_header endRefreshing];
        }];
        __block TopicTableView *topic=self;
        self.pagNum = 0;
        self.mj_footer=[RefershHelper footerWithRefreshingBlock:^{
            topic.pagNum ++;
//             NSLog(@"%ld",(long)topic.pagNum);
            [[YZENetwork sharedInstace]getRequestWithUrl:[NSString stringWithFormat:@"%@%@?page=%ld", BASE, PageUrl,(long)topic.pagNum]
                                              parameters:nil
                                                delegate:self
                                              requestTag:1
                                            successBlock:^(id responseObject)
            {
                if (responseObject == nil || ((NSArray*)responseObject).count==0) {
                    self.footRefreshState = MJFooterRefreshStateNoMore;
                }
                NSMutableArray *arr=[YZEModel mj_objectArrayWithKeyValuesArrayAdd:responseObject];
                [topic.topicArr addObjectsFromArray:arr];
                [self reloadData];
            }];
            self.footRefreshState = MJFooterRefreshStateLoadMore;
        }];
    }
    return self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.topicArr.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YZECommunityTableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"YZECommunityTableViewCell2"];
    UIView *oldPics = [cell viewWithTag:666];
    if (oldPics) {
        [oldPics removeFromSuperview];
    }
    BaseModel *model=self.topicArr[indexPath.row];
    [cell showData:model superWidth:tableView.width];
    return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    YZECommunityTableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"YZECommunityTableViewCell2"];
    CGFloat h = [cell cellHeight:self.topicArr[indexPath.row] superWidth:tableView.width];
    return h;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TopicDetailViewController *detail=[TopicDetailViewController new];
    UINavigationController *navi=[[UINavigationController alloc]initWithRootViewController:detail];
    detail.topicDetail=self.topicArr[indexPath.row];
    
    [[self viewController] presentViewController:navi animated:YES completion:nil];
}

- (UIViewController*)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}
@end
